<template>
  <div>
    <el-dialog
      :title="dialogInfo.isAdd ? '添加会员' : '编辑会员'"
      :visible.sync="dialogInfo.isShow"
      :before-close="cancel"
    >
      <el-form :rules="rules" :model="memberform" ref="memberInfo">
        <el-form-item
          label="手机号："
          :label-width="formLabelWidth"
          prop="phone"
        >
          <el-input v-model="memberform.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="角色名称："
          :label-width="formLabelWidth"
          prop="nickname"
        >
          <el-input v-model="memberform.nickname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码：" :label-width="formLabelWidth">
          <el-input v-model="memberform.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="状 态：" :label-width="formLabelWidth">
          <el-switch
            v-model="memberform.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button @click="editMember" type="primary">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getMemberInfo, getMemberEdit } from "../../util/axios";
import { mapActions } from "vuex";
export default {
  data() {
    return {
      formLabelWidth: "100px",
      memberform: {
        uid: "",
        nickname: "",
        phone: "",
        password: "",
        status: 1,
      },
      rules: {
        phone: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          { min: 11, max: 11, message: "长度在 11个字符", trigger: "blur" },
        ],
        nickname: [
          { required: true, message: "请输入昵称", trigger: "blur" },
          { min: 2, max: 8, message: "长度在 2~8个字符", trigger: "blur" },
        ],
      },
    };
  },
  props: ["dialogInfo"],
  methods: {
    ...mapActions({
      getMemberListAction: "member/getMemberListAction",
    }),
    reset() {
      this.memberform = {
        uid: "",
        nickname: "",
        phone: "",
        password: "",
        status: 1,
      };
      this.$refs.memberInfo.resetFields();
    },
    cancel() {
      this.$emit("cancel", false);
      this.reset();
    },
    lookup(uid) {
      getMemberInfo({ uid }).then((res) => {
        this.memberform = res.data.list;
        this.memberform.uid = uid;
      });
    },
    editMember() {
      this.$refs.memberInfo.validate((valid) => {
        if (valid) {
          getMemberEdit(this.memberform).then((res) => {
            if (res.data.code == 200) {
              this.getMemberListAction()
              this.cancel()
            }
          });
        }else{
          this.$message.error('验证失败！')
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
</style>
